package com.weixing.mall.provider.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.provider.model.domain.Member;
import com.weixing.mall.provider.model.dto.DeliveryAddressPCDto;
import com.weixing.mall.provider.model.query.DeliveryAddressPCQuery;
import com.weixing.mall.provider.model.vo.DeliverAddressPCVo;
import com.weixing.mall.provider.service.IDeliveryAddressService;
import com.weixing.mall.provider.service.IMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author wx
 * @Date 2019/9/16 14:35
 * @Version 1.0
 */
@RestController
@RequestMapping("deliveryAddress")
@Api(value = "后台地址管理Api",tags = "后端:地址管理")
public class AdminDeliveryAddressController extends BaseController {
    @Autowired
    private IDeliveryAddressService deliveryAddressService;

    @Autowired
    private IMemberService memberService;

    @ApiOperation(value = "删除")
    @PostMapping("del/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "地址id", paramType = "path", dataType = "long")
    })
    public Result del(@PathVariable Long id){
        return deliveryAddressService.removeById(id) ? ResultUtil.success() : ResultUtil.error();
    }

    @ApiOperation(value ="批量删除")
    @PostMapping("delAll/{ids}")
    public Result delAll(long[] ids){
        deliveryAddressService.removeByIds(Lists.newArrayList(ids));
        return ResultUtil.success();
    }

    @ApiOperation(value = "新增")
    @PostMapping("save")
    public Result savePC(@RequestBody DeliveryAddressPCDto deliveryAddressPCDto){
        Member member = memberService.selectByMobile(deliveryAddressPCDto.getMobile());
        if(member!=null){
            deliveryAddressPCDto.setMemberId(member.getId());
            Long id =deliveryAddressService.savePC(deliveryAddressPCDto);
            return ResultUtil.success(id);
        }else {
            return ResultUtil.error("会员手机号不存在");
        }

    }

    @ApiOperation(value = "查询")
    @GetMapping("")
    public Result listPC(DeliveryAddressPCQuery deliveryAddressPCQuery){
        return ResultUtil.success(deliveryAddressService.selectListPC(initMpPage(),deliveryAddressPCQuery));
    }

    @ApiOperation(value = "PC端修改查询详情")
    @PostMapping("detail/{id}")
    public Result detail(@PathVariable long id){
        return ResultUtil.success(deliveryAddressService.selectDetailPC(id));
    }

    @ApiOperation(value = "根据会员id查询收货地址")
    @GetMapping("/{memberId}")
    public Result<IPage<DeliverAddressPCVo>> agentMemberAddress(@PathVariable Long memberId){
        return ResultUtil.success(deliveryAddressService.selectByMemberIdOfPage(initMpPage(),memberId));
    }
}
